package com.smartlifev30.baiwei.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartlifev30.R;
import com.smartlifev30.baiwei.bean.Gateway;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.BaseSwipedAdapter;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.BaseSwipedViewHolder;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.core.ItemTouchHelperExtension;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import smartlifev30.com.sdk.bean.response.GatewayInfo;

/**
 * @author tys
 * @date 2018/8/23
 */

public class GatewayAdapter extends BaseSwipedAdapter<GatewayInfo, GatewayAdapter.ViewHolder> {

    private MenuClickListen menuClickListen;

    public GatewayAdapter(Context context, ArrayList<GatewayInfo> mData, ItemTouchHelperExtension itemTouchHelper) {
        super(context, mData, itemTouchHelper);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.gateway_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GatewayInfo gatewayInfo = getData(position);
        holder.name.setText(gatewayInfo.getAlias());
        holder.sn.setText(String.format(context.getResources().getString(R.string.gateway_sn),
                gatewayInfo.getSn()));
        holder.status.setText(String.format(context.getResources().getString(R.string.gateway_status), "在线"));
    }

    public GatewayAdapter setMenuClickListen(MenuClickListen menuClickListen) {
        this.menuClickListen = menuClickListen;
        return this;
    }

    public interface MenuClickListen {
        void onMenuClick(View view, int position);
    }

    public class ViewHolder extends BaseSwipedViewHolder {
        @BindView(R.id.gateway_item_action_container)
        View actionContainer;

        @BindView(R.id.gateway_item_content_container)
        View contentContainer;

        @BindView(R.id.gateway_item_name)
        TextView name;

        @BindView(R.id.gateway_item_sn)
        TextView sn;

        @BindView(R.id.gateway_item_status)
        TextView status;

        @BindView(R.id.gateway_item_action_quit)
        TextView actionDel;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.gateway_item_content_container)
        void onItemClick(View view) {
            if (mItemClickListen != null) {
                mItemClickListen.onItemClick(view, getAdapterPosition());
            }
        }

        @OnClick(R.id.gateway_item_action_quit)
        void onMenuClick(View v) {
            if (menuClickListen != null) {
                menuClickListen.onMenuClick(v, getAdapterPosition());
            }
            closeMenu();
        }


        @Override
        @NonNull
        public View getContentViewContainer() {
            return contentContainer;
        }

        @Override
        @NonNull
        public View getActionViewContainer() {
            return actionContainer;
        }
    }
}
